/**
* @file Lexem.cpp Lexem implementation.
* The lexem is used from the FSM.
*
*/

#include "Lexem.h"

	/**
	* Constructor.
	* @param size The initial size of the internal vector. 
	*/
	Lexem::Lexem(unsigned int size) {
		this->lexem = new char[size];
		this->size = size;
		this->count = 0;
	}
	
	Lexem::~Lexem() {
		delete[] this->lexem;
	}
	
	
	/**
	* Clear the Lexem.
	* It resets the length of the internal vector to zero.
	*/
	void Lexem::reset() {
		this->count = 0;
		this->lexem[0] = 0;
	}
	
	/**
	* Appends a character.
	*
	*/
	void Lexem::append(char c) {
	
		this->checkSpace();
		
		
		this->lexem[this->count++] = c;
		this->lexem[this->count] = 0;
	}
	
	/**
	* Shift the internal pointer backwards.
	* @param count The number of shifts.
	*/
	void Lexem::rewind(int count) {
		if(this->count > count) {
			this->lexem[this->count - count] = 0;
			this->count -= count;
		}
	}
	
	/**
	* Returns the current lexem.
	* @return The Lexem.
	*/
	char* Lexem::getLexem() {
		return this->lexem;
	}
	
	/**
	* Returns the length of the current lexem.
	* @return The length. 
	*/
	unsigned int Lexem::getLength() {
		return this->count;
	}
	
	/**
	* Checks if the vector is big enough.
	* If necessary, makes it bigger.
	*
	*/
	void Lexem::checkSpace() {
		// make the array bigger (*2)
		if(this->count >= this->size-1) {
			
			int newSize = this->size*2;
			char* tmp = new char [ newSize ];

			for(int i= 0 ; i < this->size;i++){
				tmp[i] = this->lexem[i];
			}

			delete[] this->lexem;
			this->lexem = tmp;

			this->size = newSize;
		}
	}
